<?php
use think\facade\Db;
use think\facade\Env;

function cate_one($id=0)
{
    if(!$id or !$cat=\app\service\CateService::where('isopen',1)->field('id,catename,en_name,fid,type,keyword')->find($id)){
        return 'id 错误！';
    }
    $cat['src']=url('index/cate',['id'=>$id]);
    return $cat;
}

//下面自己编写的方法
//获取幻灯片
//slide
function slide()
{
    if (config('app.lizhili_bug')) {
        $slide=\app\model\SlideModel::field('title,url,img,desc')->where('isopen',1)->order('update_time desc,id asc')->select()->toArray();
    } else {
        if (!$slide=cache('slide')) {
            $slide=\app\model\SlideModel::field('title,url,img,desc')->where('isopen',1)->order('update_time desc,id asc')->select()->toArray();
            cache('slide', $slide, 3600);
        }
    }
    return $slide;
    // 使用方法
    // {volist name=":slide()" id="vo"}
    // {$vo.id}
    // {/volist}
}
//列表页
function lit($id=0,$num=10){
    if ($id==0) {
        return 'id必须填写！';
    }
    $lit=\app\service\CateService::lit($id,$num);
    return $lit;
    // 使用方法
    // {volist name=":lit($id,10)" id="vo"}
    // {$vo.id}
    // {/volist}
    //默认分页
    //{:lit($id,10)->render()}
}

function LizhiliSlide($data=null,$time=3000,$type=1){
	$name='a'.uniqid();
	$shu=''; 
	if($type==1){
		foreach ($data as $k=>$v) {
		    $shu.="<div class='item'><a href='".$v['url']."' target='_blank'><img src='".$v['img']."' /></a></div>";
		}
		$html=<<<EOF
		<style>#cardBox$name {width: 100%;position: relative;overflow: hidden;}
		    #cardBox$name .imgBox {transition: all 1s;transform: translateX(0px); width: 100%;}
		    #cardBox$name .item {float: left;width: 100%;}
		    #cardBox$name .item img {width: 100%;}
		  </style>
		<div id="cardBox$name">
		    <div class="imgBox">$shu </div>
		</div>
		  <script>
			(function(){
				let card = document.querySelector('#cardBox$name .imgBox')
				let cardBox = document.querySelector('#cardBox$name')
				let width= cardBox.offsetWidth;
				let items = document.querySelectorAll("#cardBox$name .item")
				let lengt=items.length
				card.style.width=lengt * width+'px'
				if(lengt>1){
					let index = 0
					items.forEach((item, index) => {
						item.style.width=width+'px'
					  let translateX = index * width
					  item.style.left = translateX+'px'
					})
					let timer = null
					const initInterval = () => {
					  timer = setInterval(() => {
					    index++
					    let translateX = -1*index * width
					    card.style.transform = 'translateX('+translateX+'px)'
					    if (index >= lengt-1) {
					      index = -1
					    }
					  }, $time);
					}
					initInterval()
				}
			})()
		  </script>
EOF;
	}
	
	if($type==2){
		foreach ($data as $k=>$v) {
		    $shu.="<li><a href='".$v['url']."' target='_blank'><img src='".$v['img']."' /></a></li>";
		}
		$html=<<<EOF
		    <ul id="lizhili_slide$name">
					$shu
				</ul>
				<style>
					#lizhili_slide$name >li{display: none;}#lizhili_slide$name >li:first-child{display: block;}
				</style>
				<script type="text/javascript">
					(function() {
						let dom$name=document.querySelectorAll("#lizhili_slide$name li");
						if(dom$name.length>1){
							setInterval(function() {
							    for (let i = 0; i < dom$name.length; i++) {
							        if(dom{$name}[i].offsetParent){
							            for (let ii = 0; ii < dom$name.length; ii++) {dom{$name}[i].style.display='none'}
										if(i+1>=dom$name.length){
							                dom{$name}[0].style.display='block'
										}else{
							                dom{$name}[(i+1)].style.display='block'
										}
										break; 
									}
							    }
							}, $time);
						}
						
					})()
				</script>
EOF;
		    
	}
    return $html;
    
    // {:LizhiliSlide($ad.index,$time=3000,$type=1)}

    //幻灯片
    //{:LizhiliSlide(slide(),$time=3000)}
}
function SuperSlide($data=null,$time=3000,$autoPlay=true,$next=false,$num=false,$color='#f00'){
    $name='a'.uniqid();
    $shu='';
    $shudian='';
    foreach ($data as $k=>$v) {
        $shu.="<li><a href='".$v['url']."' target='_blank'><img src='".$v['img']."' /></a></li>";
        $shudian.="<li>".($k+1)."</li>";
    }
    if(!$num){
        $shudian='';
    }
    $qian='';
    if($next){
        $qian="/* 下面是前/后按钮代码，如果不需要删除即可 */
				.$name .prev,
				.$name .next{ position:absolute; left:3%; top:50%; margin-top:-25px; display:block; width:32px; height:40px; background:url(/static/slider-arrow.png) -110px 5px no-repeat; filter:alpha(opacity=50);opacity:0.5;   }
				.$name .next{ left:auto; right:3%; background-position:8px 5px; }
				.$name .prev:hover,
				.$name .next:hover{ filter:alpha(opacity=100);opacity:1;  }
				.$name .prevStop{ display:none;  }
				.$name .nextStop{ display:none;  }";
    }
    $dian='';
    if($num){
        $dian="
			.$name  .hd{ height:15px; overflow:hidden; position:absolute; right:5px; bottom:5px; z-index:1; }
			.$name  .hd ul{ overflow:hidden; zoom:1; float:left;  }
			.$name .hd ul li{ float:left; margin-right:2px;  width:15px; height:15px; line-height:14px; text-align:center; background:#fff; cursor:pointer; }
			";
    }


    $html=<<<EOF
		<style type="text/css">
				/* 本例子css */
				.$name{ width:100%; height:auto; overflow:hidden; position:relative;}
				.$name .hd ul li.on{ background:$color; color:#fff; }
				.$name .bd{ position:relative; height:100%; z-index:0;   }
				.$name .bd li{ zoom:1; vertical-align:middle;}
				.$name .bd img{ width:100%; height:auto; display:block;  }
				$qian
				$dian
				</style>
				<div class="$name">
					<div class="hd">
						<ul>
						$shudian
						</ul>
					</div>
					<div class="bd">
						<ul>
							$shu
						</ul>
					</div>
					<!-- 下面是前/后按钮代码，如果不需要删除即可 -->
					<a class="prev" href="javascript:void(0)"></a>
					<a class="next" href="javascript:void(0)"></a>
				</div>
				<script src="/static/jquery.SuperSlide.2.1.3.source.js"></script>
				<script type="text/javascript">
				let len=document.querySelectorAll('.$name .bd li').length
				if(len>1){
					jQuery(".$name").slide({mainCell:".bd ul",effect:"leftLoop",autoPlay:$autoPlay,interTime:$time});
				}
				</script>
EOF;

    return $html;
    //使用说明
    //需要提前引入 jq ，
    //参数 广告数据，间隔时间，自动播放，是否显示 上下箭头
    // {:SuperSlide($ad.index,$time=3000,$autoPlay=true,$next=false)}

    //幻灯片
    //{:SuperSlide(slide(),$time=3000,$autoPlay=true,$next=false)}
}

function hot($id=0,$num=3,$img=false,$offset=0,$order=true,$field='*',$where=true) // 热门文章
{
    return \app\service\CateService::hot($id, $num,$img,$offset,$order, $field, $where);
    //使用方法
    // {volist name=":hot($id,$num,$img,$offset,$order,$field,$where)" id="vo"}
    // {$vo.id}
    // {/volist}
}
function rec($id=0,$num=3,$img=false,$offset=0,$order=true,$field='*',$where=true) // 推荐文章
{
    return \app\service\CateService::rec($id, $num,$img,$offset,$order, $field, $where);
    //使用方法
    // {volist name=":rec($id,$num,$img,$offset,$order,$field,$where)" id="vo"}
    // {$vo.id}
    // {/volist}
}

function sui($id=0, $num=3,$img=false,  $field='*', $where=true) //随机读取文章,默认调用两级
{
    return \app\service\CateService::sui($id, $num,$img, $field, $where);
    //使用方法
    // {volist name=":sui($id,$num,$img,$field,$where)" id="vo"}
    // {$vo.id}
    // {/volist}
}

function cate($id=0, $num=3, $offset=0,$order=true,$field='*', $where=true)
{
    return \app\service\CateService::cate($id, $num, $offset,$order,$field, $where);
    //使用方法 //单个栏目
    // {volist name=":cate($id,$num,$offset,$order,$field,$where)" id="vo"}
    // {$vo.id}
    // {/volist}
}
function cate_main($id=0, $nums=[5],$order=true,$field='*', $where=true)
{
    if(!is_array($nums)){
        return '数量必须为数组！';
    }
    if(!$id or !\app\model\CateModel::where('id',$id)->where('isopen',1)->field('id')->find()){
        return '传入id，找不到数据！';
    }
    return \app\service\CateService::cate_main($id, $nums,$order,$field, $where);
    //使用方法 //单个栏目
    // {volist name=":cate_main($id,$num,$offset,$order,$field,$where)" id="vo"}
    // {$vo.id}
    // {/volist}
}

function cates($ids=[], $num=3,$order=true,$field='*', $where=true)
{
    return \app\service\CateService::cates($ids, $num, $order,$field, $where);
    //使用方法 //调用多个同级栏目
    // {volist name=":cates([]$ids,$num,$order,$field,$where)" id="vo"}
    // {$vo.id}
    // {/volist}
}
function cateall($ids=[], $num=[3],$unids=[],$order=true,$field='*', $where=true)
{
    return \app\service\CateService::cateall($ids, $num,$unids, $order,$field, $where);
    //使用方法 //两层循环 用于首页大循环
    // {volist name=":cateall([]$ids,[]$num,[]$unids,$order,$field,$where)" id="vo"}
    // {$vo.id}
    // {/volist}
}

function catelist($ids=[], $num=3,$order=true,$field='*', $where=true)
{
    return \app\service\CateService::catelist($ids, $num, $order,$field, $where);
    //使用方法  同时调用多个栏目
    // {volist name=":catelist([]$ids,$num,$order,$field,$where)" id="vo"}
    // {$vo.id}
    // {/volist}
}
function friend($num=10000)
{
    if (config('app.lizhili_bug')) {
        $friend=\app\model\LinkModel::where('isopen', 1)->limit($num)->order('sort asc,id desc')->select()->toArray();
    } else {
        if (!$friend=cache('friend')) {
            $friend=\app\model\LinkModel::where('isopen', 1)->order('sort asc,id desc')->limit($num)->select()->toArray();
            cache('friend', $friend, 3600);
        }
    }
    return $friend;
    // 使用方法
    // {volist name=":friend($num)" id="vo"}
    // {$vo.id}
    // {/volist}
}
function shuoming($num=0,$unids=[])
{
    if (config('app.lizhili_bug')) {
        if($num){
            $shuomimg=\app\model\ShuomingModel::where('isopen', 1)->whereNotIn('id',$unids)->order('id desc')->limit($num)->select()->toArray();
        }else{
            $shuomimg=\app\model\ShuomingModel::where('isopen', 1)->whereNotIn('id',$unids)->order('id desc')->select()->toArray();
        }
    } else {
        if (!$shuomimg=cache('friend')) {
            if($num){
                $shuomimg=\app\model\ShuomingModel::where('isopen', 1)->whereNotIn('id',$unids)->order('id desc')->limit($num)->select()->toArray();
            }else{
                $shuomimg=\app\model\ShuomingModel::where('isopen', 1)->whereNotIn('id',$unids)->order('id desc')->select()->toArray();
            }
            cache('friend', $shuomimg, 3600);
        }
    }
    return $shuomimg;
    // 使用方法
    // {volist name=":shuomimg($num=0,$uniids=[])" id="vo"}
    // {$vo.id}
    // {/volist}
}
function nav($fid=0,$type=false)//第二参数，是否平铺
{
    if (config('app.lizhili_bug')) {
        if($type){
            $cate = \app\model\CateModel::field('id,catename,en_name,type,url,mark')->where('isopen', 1)->order('sort asc')->select()->toArray();
        }else{
            $cate = \app\model\CateModel::where('fid', $fid)->field('id,catename,en_name,type,url,mark')->where('isopen', 1)->order('sort asc')->select()->toArray();
            foreach ($cate as $k=>$v) {
                $cate[$k]['zi']=\app\model\CateModel::where('fid', $v['id'])->field('id,catename,en_name,type,url,mark')->order('sort asc')->where('isopen', 1)->select()->toArray();
            }
        }

    } else {
        if (!$cate=cache('cate'.$fid)) {
            if($type){

                $cate = \app\model\CateModel::field('id,catename,en_name,type,url')->where('isopen', 1)->order('sort asc')->select()->toArray();
            }else{
                $cate = \app\model\CateModel::where('fid', $fid)->field('id,catename,en_name,type,url')->where('isopen', 1)->order('sort asc')->select()->toArray();
                foreach ($cate as $k=>$v) {
                    $cate[$k]['zi']=\app\model\CateModel::where('fid', $v['id'])->field('id,catename,en_name,type,url')->order('sort asc')->where('isopen', 1)->select()->toArray();
                }
            }
            cache('cate'.$fid, $cate, 3600);
        }
    }
    return $cate;
    // 使用方法
    // {volist name=":nav()" id="vo"}
    // {$vo.id}
    // {/volist}
}
function lay_breadcrumb($fen='>'){ //面包屑导航

    $controller=request()->controller;
    $action=strtolower(request()->action);

    if($controller!='app\controller\IndexController'){
        return '控制器必须为index';
    }
    if($action!='show' and $action!='cate'){
        return '方法必须为show 或者 cate';
    }
    $breadcrumb=\app\service\CateService::breadcrumb($controller,$action,input('id'));

    $html='<span class="layui-breadcrumb" lay-separator="'.$fen.'">';
    $html.= '<a href="/">首页</a>' .$fen;
    foreach ($breadcrumb as $k=>$v) {
        $html.="<a href='/channel/{$v['id']}.html'>{$v['catename']}</a>";
    }
    if($action=='show'){
        $html.="<a><cite>正文</cite></a>";
    }
    $html.='</span>';

    return $html;
    // 使用方法
    // {:lay_breadcrumb(' > ')} boot默认不需要传值
}

function boot_breadcrumb($fen=''){ //面包屑导航
    $controller=strtolower(request()->controller());
    $action=strtolower(request()->action());

    if($controller!='index'){
        return '控制器必须为index';
    }
    if($action!='show' and $action!='cate'){
        return '方法必须为show 或者 cate';
    }
    $breadcrumb=\app\service\CateService::breadcrumb($controller,$action,input('id'));

    $html='<ul class="breadcrumb">';
    $html.= '<li><a href="/">首页</a></li>' .$fen;
    foreach ($breadcrumb as $k=>$v) {
        $html.="<li><a href='/channel/{$v['id']}.html'>{$v['catename']}</a></li>$fen";
    }
    if($action=='show'){
        $html.="<li class='active'>正文</li>";
    }else{
        $html=substr($html, 0, -(strlen($fen)));
    }
    $html.='</ul>';
    if($fen){
        $sty= <<<___
<style type="text/css">
	.breadcrumb > li + li:before{
		content:none
	}
	.breadcrumb li{
		padding: 0 8px;
	}
</style>
___;
        $html.=$sty;
    }
    return $html;
    // 使用方法
    // {:boot_breadcrumb(' > ')} boot默认不需要传值
}
//获取下载文件
function xia($idarr=[],$num=3,$offset=0,$order=false,$field='*', $where=true)
{
    return \app\service\CateService::xia($idarr,$num,$offset,$order,$field, $where);
    //使用方法
    // {volist name=":xia([]$idarr,$num,$offset,$order,$field,$where)" id="vo"}
    // {$vo.id}
    // {/volist}
}
